package com.sun;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.SynchronizedTest02")
public class SynchronizedTest02 {
    static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        Room room = new Room();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                room.increment();
            }
        }
        );
        Thread t2 = new Thread(
                () -> {
                    for (int i = 0; i < 10; i++) {
                        room.decrement();
                    }
                }
        );
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        log.info("count={}",room.getCount());
    }
}

class Room {
    private int count = 0;

    public void increment() {
        synchronized (this) {
            count++;
        }
    }

    public void decrement() {
        synchronized (this) {
            count--;
        }
    }

    public int getCount() {
        return count;
    }
}
